<template>
	<view >
		<custom-navigation-bar title="商品列表"></custom-navigation-bar> 
		<view class="top01" > 
			<view class="top02"> 
				<view class='ok_btn' style="border: 3rpx solid white;margin-bottom: 15rpx;margin-left: 20rpx;" @click="add()">添加</view>
			</view>
			 <view style="width: 100%;float: left;height: 15rpx;"></view>
		</view> 
		<view class="top05-1"></view>  
	  
		<!-- 商品管理 -->
		<view v-if="goods.length>0">
			<view class="list-item" v-for="(item,index) in goods" :key="index" >
				<view class="list-shop-img">
					<image :src="item.thumbnail" class="list-shop-img1"></image>
				</view>
				<view class="list-item-content" style="height: auto;">
					<view class="list-item-content-title">
						{{item.name}}
						<label v-if="item.status==1" class="status_fail">报名中</label>
						<label v-if="item.status==2" class="status_fail">已完成</label>
						<label v-if="item.status==3" class="status_cussess">未成形</label>
						<label v-if="item.status==4" class="status_fail">已删除</label> 
					</view>
					<view class="list-item-content-v">
						承办方：{{item.organizer}}
					</view> 
					<view class="list-item-content-v">
						售价：{{item.price}}
					</view>
					<view class="list-item-content-v">
						创建时间：{{item.createTime}}
					</view>
					<view class="list-item-content-v">
						报名截止时间：{{item.deadline}}
					</view>
					<view class="list-item-content-v">
						活动开始时间：{{item.holdingTime}}
					</view>
					
				</view> 
				<view class="list-item-content-v">
					&nbsp;&nbsp;重复购买：<text v-if="item.isRepeat==1">允许</text><text v-if="item.isRepeat==2">不允许</text>  &nbsp;&nbsp;消费时间：{{item.useTime}}
				</view>
				<view class="list-item-btn-box" v-if="item.status>=0"> 
					<view class="list-item-btn" @click="get(item)">
						查看商品
					</view>
					<view class="list-item-btn" @click="changeStatus(item.id,-1)">
						删除商品
					</view> 
					<view v-if="item.status==1" class="list-item-btn" @click="changeStatus(item.id,3)">
						商品下架
					</view>
					<view v-if="item.status==3" class="list-item-btn" @click="changeStatus(item.id,1)">
						商品上架
					</view>
				</view>
			</view>
		</view>
		<view v-if="goods.length==0" class="more">
			暂无数据
		</view>
		<view style="width: 100%;height: 60rpx;float: left;"></view>
	</view>
</template>

<script setup>
	import CustomNavigationBar from '@/components/CustomNavigationBar.vue'; 
	import { ref,onMounted, onUnmounted } from "vue";  
	import http from '../../../utils/http';
	const modalVisible=ref(false);
	//商品列表
	const goods=ref([]);
	function add(){
		uni.navigateTo({
			url: "/pages/my/goods/goods"
		})
	}

	function get(item){
		uni.navigateTo({
			url: "/pages/my/goods/goods?id="+item.id
		})
	}
	function changeStatus(id,status){
		var msg="确定删除？";
		if(status==1){
			msg="确定上架商品？";
		}
		if(status==3){
			msg="确定下架商品？";
		}
		uni.showModal({
		    title: '提示',
		    content: msg,
		    success: (res) => {
				if (res.confirm) {
				    http.request({
				    	url: "/shop/goods/change/goods/status",
				    	method: "POST",
				    	data: {
				    		"userId":getApp().globalData.shopUserId,
				    		id:id,
				    		status:status
				    	}
				    }).then((responseData) => { 
				    	 if(responseData.code==1){
				    		 uni.showToast({
				    		 	icon: 'success',
				    		 	title: '设置成功'
				    		 })
				    		 getGoods()
				    	 }else if(responseData.code==-1){
				    		 uni.navigateTo({
				    		 	url: "/pages/my/shop/login"
				    		 })
				    	 }else{
				    		uni.showToast({
				    			icon: 'error',
				    			title: responseData.message
				    		}) 
				    	 }
				    }).catch((err) => {
				    	uni.showToast({
				    		icon: 'error',
				    		title: '请求失败'
				    	})
				    })
				} else if (res.cancel) {
				      console.log('用户点击取消');
				} 
		    }
		});
	}
	//获取数据列表
	function getGoods(){ 
		http.request({
			url: "/shop/goods/goods/list",
			method: "POST",
			data: {
				"userId":getApp().globalData.shopUserId,
			}
		}).then((responseData) => { 
			 if(responseData.code==1){
				 console.log(responseData.data)
				 for(var i=0;i<responseData.data.data.length;i++){
					 if(responseData.data.data[i].endTime){
						responseData.data.data[i].endTime=responseData.data.data[i].endTime.substring(0,10); 
					 }
				 }
				 goods.value=responseData.data.data;
				 console.log(goods.value)
			 }
		}).catch((err) => {
			uni.showToast({
				icon: 'error',
				title: '请求失败'
			})
		})
	}
	getGoods();  
	defineExpose({
		getGoods
	}) 
</script>

<style> 
	@import "../../list/list.scss";
	.top05-1{
		width: 100%;height: 260rpx;float: left;
	}
	.more{
		width: 100%;text-align: center;line-height: 50rpx;color: #666666;font-size: 25rpx;float: left;margin-top: 10rpx;
	}
</style>